<!-- 
All rights Reserved, Designed By www.youyacao.com 
@Description:积分充值页面
@author:成都市一颗优雅草科技有限公司     
@version V5.x 
注意：本前端源码遵循 MulanPSL-2.0开源协议（木兰宽松许可证）本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   企业客服QQ:2853810243  官方客户·技术交流群 929353806交流群 929353806
 -->
<template>
	<view>
		<view class="content-wrapper">
			<view class="top-black"></view>
			<view class="top-card-wrapper">
				<view class="top-card-info" :style="{backgroundImage:`url(${indexBackgroundImage})`,backgroundSize: 'cover'}">
					<image class="head-img" :src="user!=null?formatUrl(user.head_img,5):'/static/logo.png'" mode="aspectFill"></image>
					<view class="head-right">
						<view class="head-right-name-wrapper">
							<text class="vip-username">{{user!=null?user.name:"请登录"}}</text>
							<view class="vip-icon-wrapper" v-show="is_active">
								<image src="../../../static/icon/vip-zuan@2x.png" mode="aspectFill"></image>
								<text>VIP</text>
							</view>
						</view>
						<text class="vip-end-time">VIP会员到期时间:{{user!=null?(user.vip_end==undefined?"未开通":user.vip_end):"未开通"}}</text>
					</view>
				</view>
			</view>

		</view>
		<view class="pay-content-wrapper">
			<view class="pay-content-card-wrapper">
				<text class="pay-title">输入金币数量</text>
				<view class="pay-item-list">
					<view class="uni-form-item uni-column">
						<input class="uni-input" v-model="gold" placeholder="1元=10金币" />
					</view>
				</view>
				<text class="pay-title">支付方式</text>
				<view class="pay-item-list pay-type-item-list">
					<view class="pay-item pay-type-item" :class="{selected:payTypeSelected==item.fname}" v-for="(item,index) in payTypeItemList"
					 :key="item.fname" @click="changePayTypeItem(item.fname)">
						<image class="pay-img" :src="formatUrl(item.icon)"></image>
						<text class="pay-item-name">{{item.name}}</text>
						<view class="pay-selected">
							<image src="../../../static/img/select.png"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="pay-btn-wrapper">
				<text class="pay-btn" @tap="buy">
					确认充值
				</text>
			</view>
		</view>
	</view>
</template>

<script>
	import refresh from '@/components/refresh/refresh.vue'
	import indexBackgroundImage from "@/static/img/VIP-back.png"
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	const picker = weex.requireModule('picker')
	import NP from 'number-precision'	
	export default {
		components: {
			uniIcons,
		},
		data() {
			return {
				payitemSelected: 1,
				payItemList: [],
				payTypeSelected: 'alipay',
				payTypeItemList: [],
				indexBackgroundImage: indexBackgroundImage,
				isMore: true,
				loadmore: false,
				is_active: false,
				gold: ''
			}
		},
		created() {
			this.$nextTick(() => {
				setTimeout(() => {
					if (this.isVip()) {
						this.is_active = true;
					}
				}, 500)
			})
		},
		methods: {
			isVip() {
				if (this.user == null) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}
				return this.user.is_vip;
			},
			pick(_gold) {
				picker.pick({
					items: ['微信', '支付宝']
				}, event => {
					let _type = 'wxpay'
					if (event.result === 'success') {
						if (event.data) {
							_type = 'alipay'
						} else {
							_type = 'wxpay'
						}
						uni.navigateTo({
							url: `/pages/pet-pay/pet-pay?amount=${NP.divide(_gold, 10)}&type=${_type}`
						})
					}
				})
			},
			buy() {
				let gold = this.gold
				let payType = this.payTypeSelected
				if (gold === '' || gold === 0) {
					return uni.showToast({
						title: '金币数量不能为空',
						icon: 'none'
					})
				}
				if (payType === 'pet') {
					this.pick(gold)
				} else {
					this.$api.codePayIntegral({
						gold,
						payType
					}).then((res) => {
						if (res.data.code == 1) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						} else {
							uni.redirectTo({
								url: "pay?url=" + encodeURIComponent(res.data.data.codeUrl)
							})
						}
						uni.redirectTo({
							url: "pay?url=" + encodeURIComponent(res.data.data.codeUrl)
						})
					})
				}
			},
			getData() {
				this.getInfo();
				this.getVipList();
				this.getTypeList();
			},
			getVipList() {
				this.$api.vipShopList().then((res) => {
					if (res.data.code == 1) {
						uni.showModal({
							title: "获取失败",
							content: res.data.msg,
							showCancel: false,
							cancelText: '',
							confirmText: '确定',
							success: res => {
								this.back();
							},
							fail: () => {},
							complete: () => {}
						});
						return;
					}
					this.payItemList = res.data.data;
				})
			},
			getTypeList() {
				this.$api.paytypeList().then((res) => {
					if (res.data.code == 1) {
						uni.showModal({
							title: "获取失败",
							content: res.data.msg,
							showCancel: false,
							cancelText: '',
							confirmText: '确定',
							success: res => {
								this.back();
							},
							fail: () => {},
							complete: () => {}
						});
					}
					this.payTypeItemList = res.data.data;
				})
			},
			//格式化链接地址
			formatUrl(url1) {
				return this.$utils.formatUrl(this, url1)
			},
			changePayItem(index) {
				console.log(index);
				this.payitemSelected = index;
			},
			changePayTypeItem(index) {
				this.payTypeSelected = index;
			},
			getInfo() {
				//用户未登录直接返回
				if (this.user == null) {
					return;
				}
				//获取用户信息
				this.$store.dispatch('getUserInfo');
			},
			seach() {

				uni.navigateTo({
					url: '/pages/index/user/category-search/category-search',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			//格式化链接地址
			formatUrl(url) {
				if (url == undefined) {
					return;
				}
				var index = url.indexOf("http");
				if (index == 0) {
					return url;
				}
				return this.$store.state.baseUrl + url;
			},
			refresh() {
				this.getInfo();
				this.$refs.refresh.endAfter() //刷新结束调用

			}
		},
		computed: {
			user() {
				return this.$store.state.user;
			}
		},
		mounted() {
			this.getData();
		}
	}
</script>

<style lang="scss">
	.head-wrapper {
		align-items: center;
		position: fixed;
		//#ifdef APP-PLUS
		padding-top: 44rpx;
		//#endif
		z-index: 1;
		left: 0;
		right: 0;
		background: rgba(44, 44, 44, 1);
		width: 100%;
		display: flex;
		flex-direction: row;
		height: 88rpx;

		.head-title {
			font-size: 38rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: rgba(255, 255, 255, 1);
		}
	}

	.content-wrapper {
		position: relative;
		padding-bottom: 123rpx;
		//#ifdef APP-PLUS
		//#endif
		overflow: hidden;
		height: 100%;
		width: 100%;

		.top-black {

			z-index: 2;
			position: relative;
			height: 160rpx;
			width: 100vw;
			text-align: center;
		}

		.top-black:after {
			content: "";
			position: absolute;
			left: -20%;
			top: 0;
			background: rgba(44, 44, 44, 1);
			height: 160rpx;
			width: 140vw;
			border-radius: 0 0 50% 50%;
		}
	}

	.shadow {
		box-shadow: 0 1px 6px #ccc;
	}

	.title-wrap {
		flex: 11;
		display: flex;
		flex-direction: row;
		justify-content: center;
	}


	.seach {
		flex: 1;
		display: flex;
		flex-direction: row;
		position: relative;
		right: 0rpx;
		justify-content: flex-end;
		align-items: center;

		image {
			display: inline-block;
			width: 35rpx;
			height: 25rpx;
			margin-right: 25rpx;

		}
	}


	.left-wrapper {
		margin-left: 12rpx;
		height: 88rpx;
		line-height: 88rpx;
		flex-direction: row;
		// background: #007AFF;
		justify-content: center;
		flex: 1;
	}

	.search-wrapper {
		text-align: center;
		line-height: 76rpx;
		height: 76rpx;
		//background: #09BB07;
		flex: 10;
		flex-direction: row;
		align-items: center;
		align-content: center;
		justify-content: center;

		text {
			line-height: 76rpx;
			height: 76rpx;
		}
	}

	.top-card-wrapper {
		position: relative;
		z-index: 2;
		margin-top: -128rpx;
		left: 0;
		height: 100%;
		width: 100vw;
		display: flex;
		flex-direction: row;
		justify-content: center;

		.top-card-info {
			padding: 37rpx 40rpx;
			border-radius: 10rpx;
			display: flex;
			flex-direction: row;
			height: 100%;
			width: 100%;
			margin-left: 48rpx;
			margin-right: 48rpx;
			background-color: #f0d7a3;

			background-size: 100% 100%;
			align-items: center;

			.head-img {
				border-radius: 50%;
				height: 110rpx;
				width: 110rpx;
			}
		}

		text {}

		.head-right {
			margin-left: 24rpx;

			.head-right-name-wrapper {
				display: flex;
				flex-direction: row;

				.vip-username {
					font-size: 36rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: rgba(142, 62, 3, 1);
					margin-bottom: 8rpx;
				}
			}

		}

		.vip-end-time {

			font-size: 26rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: rgba(142, 62, 3, 1);
		}

		.vip-icon-wrapper {
			margin-left: 14rpx;
			background: rgba(255, 242, 198, 1);
			border-radius: 15rpx;
			height: 40rpx;
			width: 100rpx;
			text-align: center;
			line-height: 40rpx;

			text {
				margin-left: 8rpx;
				font-size: 14px;
				font-family: PingFang SC;
				font-weight: 400;
				color: rgba(142, 62, 3, 1);
			}

			image {
				height: 22rpx;
				width: 25rpx;
			}
		}
	}

	.pay-content-wrapper {
		z-index: 1;
		margin-top: -242rpx;
		width: 100vw;

		.pay-content-card-wrapper {
			margin-left: 24rpx;
			margin-right: 24rpx;
			padding-top: 124rpx;
			padding-left: 24rpx;
			padding-right: 12rpx;
			padding-bottom: 24rpx;
			height: 100%;
			background: rgba(255, 255, 255, 1);
			border-radius: 20rpx;

			.pay-title {
				display: inline-block;
				margin-top: 24rpx;
				margin-bottom: 24rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: rgba(102, 102, 102, 1);
			}

			.pay-item-list {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				justify-content: space-between;
				align-content: flex-start;
				align-items: flex-start;

				.pay-item {
					&.selected {
						background: rgba(255, 251, 245, 1);
						border: 1px solid rgba(218, 170, 103, 1);

						.pay-selected {
							display: flex;
							justify-content: center;
							align-items: center;
							width: 44rpx;
							height: 29rpx;
							background: rgba(218, 170, 103, 1);
							border-radius: 12px;

							image {
								width: 24rpx;
								height: 17rpx;
							}
						}
					}

					flex: 1;
					position: relative;
					margin-bottom: 24rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					align-content: center;
					min-width: 30%;
					max-width: 30%;
					margin-right: 12rpx;
					height: 159rpx;
					border: 1px solid rgba(153, 153, 153, .5);
					border-radius: 12px;

					.pay-img {
						display: inline-block;
						width: 57rpx;
						height: 57rpx;
					}

					.pay-item-name {
						display: inline-block;
						font-size: 34rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: rgba(51, 51, 51, 1);
					}

					.pay-item-price-type {
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: rgba(218, 170, 103, 1);
					}

					.pay-item-price {
						font-size: 50rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: rgba(218, 170, 103, 1);
					}

					.pay-selected {
						position: absolute;
						bottom: 0;
						right: 0;
					}

					.pay-selected {
						display: none;
						width: 44rpx;
						height: 29rpx;
						background: rgba(218, 170, 103, 1);
						border-radius: 12px;

						image {}
					}




				}
			}

			.pay-type-item-list {
				justify-content: flex-start !important;

				.pay-type-item {
					.pay-item-name {
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: rgba(51, 51, 51, 1);
						margin-left: 20rpx;
					}

					flex-direction: row;
					height:89rpx;
					margin-right: 24rpx;
				}
			}
		}

		.pay-btn-wrapper {
			text-align: center;

			.pay-btn {
				display: inline-block;
				font-size: 38rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: rgba(255, 255, 255, 1);
				margin: 64rpx auto;
				width: 350rpx;
				height: 76rpx;
				text-align: center;
				line-height: 76rpx;
				background: rgba(218, 170, 103, 1);
				border-radius: 38rpx;
			}
		}

	}
</style>
